package org.mobicents.protocols.ss7.map;

import javax.naming.InitialContext;
import javax.naming.NamingException;

import org.mobicents.protocols.ss7.map.api.MAPDialog;
import org.mobicents.protocols.ss7.map.api.MAPDialogListener;
import org.mobicents.protocols.ss7.map.api.MAPException;
import org.mobicents.protocols.ss7.map.api.MAPMessage;
import org.mobicents.protocols.ss7.map.api.MAPParameterFactory;
import org.mobicents.protocols.ss7.map.api.MAPProvider;
import org.mobicents.protocols.ss7.map.api.datacoding.CBSDataCodingScheme;
import org.mobicents.protocols.ss7.map.api.dialog.MAPAbortProviderReason;
import org.mobicents.protocols.ss7.map.api.dialog.MAPAbortSource;
import org.mobicents.protocols.ss7.map.api.dialog.MAPNoticeProblemDiagnostic;
import org.mobicents.protocols.ss7.map.api.dialog.MAPRefuseReason;
import org.mobicents.protocols.ss7.map.api.dialog.MAPUserAbortChoice;
import org.mobicents.protocols.ss7.map.api.errors.MAPErrorMessage;
import org.mobicents.protocols.ss7.map.api.primitives.AddressString;
import org.mobicents.protocols.ss7.map.api.primitives.IMSI;
import org.mobicents.protocols.ss7.map.api.primitives.MAPExtensionContainer;
import org.mobicents.protocols.ss7.map.api.primitives.USSDString;
import org.mobicents.protocols.ss7.map.api.service.supplementary.MAPDialogSupplementary;
import org.mobicents.protocols.ss7.map.api.service.supplementary.MAPServiceSupplementaryListener;
import org.mobicents.protocols.ss7.map.api.service.supplementary.ProcessUnstructuredSSRequest;
import org.mobicents.protocols.ss7.map.api.service.supplementary.ProcessUnstructuredSSResponse;
import org.mobicents.protocols.ss7.map.api.service.supplementary.UnstructuredSSNotifyRequest;
import org.mobicents.protocols.ss7.map.api.service.supplementary.UnstructuredSSNotifyResponse;
import org.mobicents.protocols.ss7.map.api.service.supplementary.UnstructuredSSRequest;
import org.mobicents.protocols.ss7.map.api.service.supplementary.UnstructuredSSResponse;
import org.mobicents.protocols.ss7.map.datacoding.CBSDataCodingSchemeImpl;
import org.mobicents.protocols.ss7.tcap.asn.ApplicationContextName;
import org.mobicents.protocols.ss7.tcap.asn.comp.Problem;

/**
 * A simple example show-casing how to use MAP stack. Demonstrates how to listen
 * to incoming Dialog from peer and process the MAP messages and send response.
 * 
 * @author Amit Bhayani
 * 
 */
public class UssdServerExample implements MAPDialogListener, MAPServiceSupplementaryListener {

	private MAPProvider mapProvider;
	private MAPParameterFactory paramFact;

	public UssdServerExample() throws NamingException {
		InitialContext ctx = new InitialContext();
		try {
			String providerJndiName = "java:/mobicents/ss7/map";
			this.mapProvider = ((MAPProvider) ctx.lookup(providerJndiName));
		} finally {
			ctx.close();
		}
	}

	public MAPProvider getMAPProvider() {
		return mapProvider;
	}

	public void start() {
		// Listen for Dialog events
		mapProvider.addMAPDialogListener(this);
		// Listen for USSD related messages
		mapProvider.getMAPServiceSupplementary().addMAPServiceListener(this);

		// Make the supplementary service activated
		mapProvider.getMAPServiceSupplementary().acivate();
	}

	public void stop() {
		mapProvider.getMAPServiceSupplementary().deactivate();
	}

	public void onProcessUnstructuredSSRequest(ProcessUnstructuredSSRequest ind) {

		USSDString ussdString = ind.getUSSDString();
		MAPDialogSupplementary currentMapDialog = ind.getMAPDialog();

		try {
			String request = ussdString.getString(null);

			// processing USSD request
			String response = "Your balans is 100$";

			CBSDataCodingScheme ussdDataCodingScheme = new CBSDataCodingSchemeImpl(0x0f);
			USSDString ussdResponse = paramFact.createUSSDString(response, null, null);

			currentMapDialog.addProcessUnstructuredSSResponse(ind.getInvokeId(), ussdDataCodingScheme, ussdResponse);
		} catch (MAPException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
	}

	public void onDialogDelimiter(MAPDialog mapDialog) {
		// This will initiate the TC-END with ReturnResultLast component
		try {
			mapDialog.send();
		} catch (MAPException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public void onErrorComponent(MAPDialog mapDialog, Long invokeId, MAPErrorMessage mapErrorMessage) {
		// TODO Auto-generated method stub

	}

	public void onRejectComponent(MAPDialog mapDialog, Long invokeId, Problem problem, boolean isLocalOriginated) {
		// TODO Auto-generated method stub

	}

	public void onInvokeTimeout(MAPDialog mapDialog, Long invokeId) {
		// TODO Auto-generated method stub

	}

	public void onMAPMessage(MAPMessage mapMessage) {
		// TODO Auto-generated method stub

	}

	public void onProcessUnstructuredSSResponse(ProcessUnstructuredSSResponse ind) {
		// TODO Auto-generated method stub

	}

	public void onUnstructuredSSRequest(UnstructuredSSRequest unstrReqInd) {
		// TODO Auto-generated method stub

	}

	public void onUnstructuredSSResponse(UnstructuredSSResponse unstrResInd) {
		// TODO Auto-generated method stub

	}

	public void onUnstructuredSSNotifyRequest(UnstructuredSSNotifyRequest unstrNotifyInd) {
		// TODO Auto-generated method stub

	}

	public void onUnstructuredSSNotifyResponse(UnstructuredSSNotifyResponse unstrNotifyInd) {
		// TODO Auto-generated method stub

	}

	public void onDialogRequest(MAPDialog mapDialog, AddressString destReference, AddressString origReference,
			MAPExtensionContainer extensionContainer) {
		// TODO Auto-generated method stub

	}

	public void onDialogRequestEricsson(MAPDialog mapDialog, AddressString destReference, AddressString origReference,
			IMSI eriImsi, AddressString eriVlrNo) {
		// TODO Auto-generated method stub

	}

	public void onDialogAccept(MAPDialog mapDialog, MAPExtensionContainer extensionContainer) {
		// TODO Auto-generated method stub

	}

	public void onDialogReject(MAPDialog mapDialog, MAPRefuseReason refuseReason,
			ApplicationContextName alternativeApplicationContext, MAPExtensionContainer extensionContainer) {
		// TODO Auto-generated method stub

	}

	public void onDialogUserAbort(MAPDialog mapDialog, MAPUserAbortChoice userReason,
			MAPExtensionContainer extensionContainer) {
		// TODO Auto-generated method stub

	}

	public void onDialogProviderAbort(MAPDialog mapDialog, MAPAbortProviderReason abortProviderReason,
			MAPAbortSource abortSource, MAPExtensionContainer extensionContainer) {
		// TODO Auto-generated method stub

	}

	public void onDialogClose(MAPDialog mapDialog) {
		// TODO Auto-generated method stub

	}

	public void onDialogNotice(MAPDialog mapDialog, MAPNoticeProblemDiagnostic noticeProblemDiagnostic) {
		// TODO Auto-generated method stub

	}

	public void onDialogRelease(MAPDialog mapDialog) {
	}

	public void onDialogTimeout(MAPDialog mapDialog) {
		// TODO Auto-generated method stub

	}
}
